import scrapy
from ..items import BiguoItem,OptionsItem,DataItem
import time

#输出命令
# scrapy crawl mulchoice -o urls.json -s FEED_EXPORT_ENCODING=utf-8

class MulchoiseSpider(scrapy.Spider):
    name = 'mulchoice'
    allowed_domains = ['https://www.biguotk.com/']
    start_urls = ["https://www.biguotk.com/topic.html?code=02628&type=1&topic_type=2",
                  "https://www.biguotk.com/topic.html?code=02323&type=1&topic_type=2",
                  "https://www.biguotk.com/topic.html?code=00910&type=1&topic_type=2",
                  "https://www.biguotk.com/topic.html?code=02378&type=1&topic_type=2",
                  "https://www.biguotk.com/topic.html?code=00024&type=1&topic_type=2",
                  "https://www.biguotk.com/topic.html?code=05679&type=1&topic_type=2"
                  ]
    URL = "https://www.biguotk.com/topic.html?code=00183&type=1&topic_type=2&id=40864"

    def start_requests(self):# def start_requests(self):  # 请求时携带Cookies

        cookies = "UM_distinctid=178e8bca758618-0d6f789098a4fa-57442618-15f900-178e8bca7594b7; CNZZDATA1279061690=1536372269-1618811903-https%253A%252F%252Fwww.baidu.com%252F%7C1618811903; laravel_session=Zke8ApW7ZStugLBSCaU2Eq11wHTupVj1qn0fmOjQ"
        cookies = {i.split('=')[0]: i.split('=')[1] for i in cookies.split('; ')}
        # for i in range(len(self.start_urls)):
            # yield scrapy.Request(self.start_urls[6], cookies=cookies, callback=self.parse)
        yield scrapy.Request(self.URL, cookies=cookies, callback=self.parse)


    def parse(self, response):


        major = response.xpath('//*[@id="professions"]/@professions_name').extract()[0]  #专业名称
        subject = response.xpath('//*[@class="title-names"]/span[3]/text()').extract()[0]  #科目
        types =  response.xpath('//*[@class="title-names"]/span[2]/text()').extract()[0]   #免费题库
        name = response.xpath('//div[@class="left-page"]/div/div/div[@class="question-type"]/span/img/@alt').extract()[0]   #题目类型
        sourse = response.xpath('/html/body/div[1]/div[2]/div/div[1]/a/div/img/@alt').extract()[0]

        test = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[2]/text()').extract()[0]   #试题
        ques_type = response.xpath('//div[@class="left-page"]/div/div/div[@class="question-type"]/span/img/@alt').extract()[0]
        sort_num = response.xpath('//*[@class="active"]/span/text()').extract()[0]
        resolution = response.xpath('//div[@class="left-page"]/div/div[2]/span/text()').extract()


        # answers = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"]/div/text()').extract()
        #正确选项
        correct_answer = response.xpath('//*[@id="answer"]/@value').extract()[0]
        print(correct_answer)

        #第一个选项信息
        name1 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][1]/div/text()').extract()[0]
        sort_num1 = 1
        A_option = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty flex-center"]/div/div[1]/div[2]/text()').extract()[0]
        print(A_option)
        if A_option in correct_answer:
            is_correct1 = 1
        else:
            is_correct1 = 0


        #第二个选项信息
        name2 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][2]/div/text()').extract()[0]
        sort_num2 = 2
        B_option = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty flex-center"]/div/div[2]/div[2]/text()').extract()[0]
        print(B_option)
        if B_option in correct_answer:
            is_correct2 = 1
        else:
            is_correct2 = 0


        #第三个选项信息
        name3 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][3]/div/text()').extract()[0]
        sort_num3 = 3
        C_option = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty flex-center"]/div/div[3]/div[2]/text()').extract()[0]
        print(C_option)
        if C_option in correct_answer:
            is_correct3 = 1
        else:
            is_correct3 = 0


        #第四个选项信息
        name4 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][4]/div/text()').extract()[0]
        sort_num4 = 4
        D_option = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty flex-center"]/div/div[4]/div[2]/text()').extract()[0]
        print(D_option)
        if D_option in correct_answer:
            is_correct4 = 1
        else:
            is_correct4 = 0


        # 第五个选项信息
        name5 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][5]/div/text()').extract()[0]
        sort_num5 = 5
        E_option = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty flex-center"]/div/div[5]/div[2]/text()').extract()[0]
        print(E_option)
        if E_option in correct_answer:
            is_correct5 = 1
        else:
            is_correct5 = 0

        #第六个选项信息
        # name6 = response.xpath('//div[@class="left-page"]/div[@class="answer-cell"]/div/div[@class="answer-choose"][6]/div/text()').extract()[0]
        # sort_num6 = 6
        # correct_answer6 = response.xpath('//div[@class="left-page"]/div/div/div[@class="flex-between top-twenty"]/div/div[6]/div[1]/@class').extract()[0]
        # if correct_answer6 == 'default default-1 correctOption-1':
        #     is_correct6 = 1
        # else:
        #     is_correct6 = 0


        #所有选项的信息
        option1 = OptionsItem(name=name1, sort_num=sort_num1, is_correct=is_correct1)

        option2 = OptionsItem(name=name2, sort_num=sort_num2, is_correct=is_correct2)

        option3 = OptionsItem(name=name3, sort_num=sort_num3, is_correct=is_correct3)

        option4 = OptionsItem(name=name4, sort_num=sort_num4, is_correct=is_correct4)

        option5 = OptionsItem(name=name5, sort_num=sort_num5, is_correct=is_correct5)

        # option6 = OptionsItem(name=name6, sort_num=sort_num6, is_correct=is_correct6)

        # options = (option1, option2, option3, option4)
        options = (option1, option2, option3, option4, option5)
        # options = (option1, option2, option3, option4, option5, option6)

        #题目的所有信息
        data = DataItem(test=test,ques_type=ques_type,sort_num=sort_num,resolution=resolution,options=options)

        items = BiguoItem(major=major,subject=subject,type=types,name=name,sourse=sourse,data=data)

        yield items

        # next_page_position = response.xpath('/html/body/div[2]/div/div[2]/div[1]/div[2]/ul/li').extract()
        time.sleep(0.2)
        # if len(next_page_position) < 9:
        next_pages = response.xpath('/html/body/div[2]/div/div[2]/div[1]/div[2]/ul/li/a[@class="next-page"]/@href').extract()
        # print(next_pages)
        # print(next_pages)
        for next_page in next_pages:
            page = 'https://www.biguotk.com/'+next_page
            page = page.replace('/ /topic','/topic',)
            print(page)
            requests =  scrapy.Request(url=page, callback=self.parse,dont_filter=True)
            yield requests


